<?php


namespace App\Models\Application;


use App\Commons\Utils\ModelTools\BaseModel;

class Application extends BaseModel {
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $searchable = [
        'created_at',
        'status',
        'name',
    ];
    
    /**
     * status 状态,1:允许访问，0:禁止访问
     */
    const STATUS_NO  = 0;
    const STATUS_YES = 1;
    
    /**
     * ip_limit_switch ip限制,1:开启ip限制，0：关闭ip限制
     */
    const IP_LIMIT_SWITCH_NO  = 0;
    const IP_LIMIT_SWITCH_YES = 1;
    
    /**
     * 生成appId，12位的36进制数最多有 pow(36, 12) 种状态
     * @return bool|string
     */
    public static function generalAppId() {
        $o = $last = '';
        do {
            $last = $o;
            usleep(10);
            $t = explode(' ', microtime());
            $o = substr(base_convert(strtr($t[0] . $t[1] . $t[1], '.', ''), 10, 36), 0, 13);
        } while ($o == $last);
        
        return $o;
    }
    
    /**
     * 秘钥生成
     * @return string
     */
    public static function generalSecretKey() {
        return md5(encrypt(time() . rand(1e7, 1e8 - 1)));
    }
}